package edu.isi.pedwork;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;

import edu.isi.pedwork.topic.LabelLDAModel;

public class Driver {

	public static String DB_CONN = Configure.dbPath;
	public static String PEDWORK_FILEPATH = "keyWords/model.txt";
	private static String path = System.getProperty("user.dir").toString()
			+ "/";
	private static Driver driver = null;

	public static void main(String args[]) {
		try {
			// LabelLDAModel.init();
			Driver driver = getInstance();

			driver.updateAll();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void updateAll() {
		try {
			init_db(DB_CONN);
			//stmt.execute("delete from user_theta");
			ResultSet rs = stmt.executeQuery("Select UserId from users");
			while (rs.next()) {
				driver.process(rs.getString("UserId"));
			}

			close_db();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static Driver getInstance() {
		if (driver == null) {
			driver = new Driver();
		}
		return driver;
	}

	private HashSet termV = null;
	private Connection conn = null;
	private Statement stmt = null;

	private Driver() {

	}

	public void init_db(String connect) throws SQLException {
		conn = DriverManager.getConnection(connect);
		stmt = conn.createStatement();
	}

	public void close_db() throws SQLException {
		stmt.close();
		conn.close();
	}

	public void process(String userId) throws Exception {
		String imodel = path + PEDWORK_FILEPATH;

		getTermVector();
		LinkedList<ArrayList<String[]>> templists = getNewData(userId);
		pedworkRun(templists, imodel, userId);
	}

	public void pedworkRun(LinkedList<ArrayList<String[]>> docFile,
			String imodel, String subwikiid) throws FileNotFoundException {
		LabelLDAModel lda = null;

		LabelLDAModel.BURN_IN = 200;
		LabelLDAModel.SAMPLE_LAG = 1;
		FileInputStream in = new FileInputStream(imodel);
		System.out.println(in);
		ObjectInputStream ois;
		try {
			ois = new ObjectInputStream(in);
			lda = (LabelLDAModel) ois.readObject();
			lda.load_inference_data_stream(docFile, 0, 1, 2);
			lda.inference_no_perplexity();
			lda.update_theta(subwikiid);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void getTermVector() throws SQLException {
		init_db(DB_CONN);
		ResultSet rs = stmt.executeQuery("SELECT termVector from termVector ");
		termV = new HashSet();
		while (rs.next()) {
			String term = rs.getString("termVector");
			termV.add(term);
		}
		close_db();
	}

	public LinkedList<ArrayList<String[]>> getNewData(String userId)
			throws Exception {
		init_db(DB_CONN);
		LinkedList<ArrayList<String[]>> lines = new LinkedList<ArrayList<String[]>>();

		stmt.execute("delete from user_theta where userId = " + userId);

		ResultSet rs = stmt
				.executeQuery("Select Tweet from tweets where UserId ="
						+ userId);

		StringBuffer doc = new StringBuffer();
		while (rs.next()) {
			String tweet = rs.getString("tweet");
			doc.append(sentenceStemming(tweet));

		}
		ArrayList<String[]> line = new ArrayList<String[]>(3);
		String[] doc_id = new String[1];
		doc_id[0] = userId;
		line.add(doc_id);
		String[] labels = new String[1];
		labels[0] = "";
		line.add(labels);
		String[] terms = doc.toString().split(" ");
		line.add(terms);
		lines.add(line);
		close_db();
		return lines;

	}

	private String sentenceStemming(String in) {
		String sentence = "";
		in = in.replace(",", " ");
		in = in.replace(".", " ");
		in = in.replace(":", " ");
		in = in.replace("!", " ");
		in = in.replace("_", " ");
		in = in.replace("-", " ");
		in = in.replace("'", " ");
		in = in.replace("\"", " ");
		in = in.replace("/", " ");

		String str[] = in.split(" ");
		for (int i = 0; i < str.length; i++) {
			String w = WordProcess.process(str[i]);
			sentence += w + " ";
			/*
			 * if (wordsMap.containsKey(w)) { wordsMap.put(w, wordsMap.get(w) +
			 * 1); } else { wordsMap.put(w, 1); }
			 */
		}

		return sentence;
	}

	public Connection getConnection(String url) throws Exception {
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		return DriverManager.getConnection(url);
	}
}
